package demo1;

class Solution {
    public String reverseStr(String s, int k) {
        int count = 0;
        char[] values = new char[s.length()];
        values = s.toCharArray();
        for (int i = 0; i < values.length; i++) {
            count++;
            if (count == 2*k) {
                temp(values,k,i);
                count = 0;
            }
        }
        if (count == 0) {
            return s.valueOf(values);
        }
        if (count < k) {
            temp1(values,values.length - count,values.length-1);
        } else if (k == count){
            temp1(values,values.length - k,values.length-1);
        } else {
            int start = values.length - count;
            int end = start + k - 1;
            temp1(values,start,end);
        }
        return s.valueOf(values);
    }
    public void temp1(char[] values, int start, int end) {
        while (start < end) {
            char tmp = values[start];
            values[start] = values[end];
            values[end] = tmp;
            start++;
            end--;
        }   
    }
    public void temp(char[] values, int k, int i) {
        int start = (i - 2 * k) + 1;
        int end = i - k;
        while (start < end) {
            char tmp = values[start];
            values[start] = values[end];
            values[end] = tmp;
            start++;
            end--;
        }
    }
}